﻿using DiaoPaiDaYin;
using DXApplicationTangche.DTO.任务DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.原型;
using System;
using System.Collections.Generic;
using System.Text;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using static DXApplicationTangche.原型.TaskDTOS;

namespace WorkFlowConsoleApp
{
    /// <summary>
    /// 关于TaskDTO可执行的工作流
    /// </summary>
    public class TaskWorkflow : IWorkflow<TaskDTO>
    {
        public string Id => "打样";

        public int Version => 1;

        public void Build(IWorkflowBuilder<TaskDTO> builder)
        {
            builder
                .StartWith<启动打样任务WorkFlow>()
                .Then<启动后续任务WorkFlow>();
        }

        public class 启动打样任务WorkFlow : StepBody
        {
            public override ExecutionResult Run(IStepExecutionContext context)
            {
                TaskDTO taskDTO = (TaskDTO)context.Workflow.Data;
                //  TODO    调用服务
                taskDTO.SaveInMTM();
                //SQLmtm.ExecuteSql("INSERT INTO `fragsmart-mtm`.`t_remark`( `order_id`, `remark`, `file_name`, `template_id`, `data`, `style_id`, `ref_style_id`, `CREATE_DATE`, `parent_id`, `version`, `principal`, `serial_number`, `status`, `A_JSON`, `A_FILE`, `CUSTOMER_ID`, `CAD_ID`, `STYLE_FIT_CD`) VALUES ( 78142, '', '78142_03732b0a3dae45ed9d53db9cd86fc31c.doc', '1', '[{\\\"title\\\":\\\"负责人\\\",\\\"value\\\":\\\"测试人1\\\"},{\\\"title\\\":\\\"时间\\\",\\\"value\\\":\\\"2020/3/11\\\"}]', 7774762, 1, '"+DateTime.Now+"', NULL, NULL, NULL, 'TR20200311000001', 'MS_01', NULL, NULL, NULL, NULL, NULL);");
                return ExecutionResult.Next();
            }
        }

        public class 启动后续任务WorkFlow : StepBody
        {
            public override ExecutionResult Run(IStepExecutionContext context)
            {
                TaskDTO taskDTO = (TaskDTO)context.Workflow.Data;

                List<ProcessDto> processDtos= ProcessService.getProcessById("打样");
                foreach (ProcessDto processDto in processDtos) {
                    new TaskDTO(taskDTO, processDto.USER_TASK).SaveInMTM();
                }
                return ExecutionResult.Next();
            }
        }
    }
}
